//	SERVER-SIDE MODULE
//	(PLEASE DO NOT MODIFY THIS CODE)
//
//	The JS proxy to the Free The Air Web Service (FTA_WS).
//
//	It exposes a set of verbs each which correspond to their server-side
//	equivalent. Some verbs include:
//
//	searchStore ().
//
//	USAGE:
//	Pass the constructor an application-level object, whose callback functions
//	will be invoked to accept any responses from the web service.
//	The name of these callback functions are identical to the name of the
//	verb.
//
//	For example, consider SEARCH_STORE.
//
//	Your application code would look as follows:
//	var	fta_ws = new FTA_WS (myAppObject);
//	fta_ws.searchStore (...);		<<- sends the SEARCH_STORE request
//	myAppObject.searchStore (...)	<<- receives the SEARCH_STORE response
//
//	The interface is designed to be comfortable to a JS application developer
//	and is shielded from the low-level wire-protocol implementation mucky details.


//
//	PUBLIC
//

function FTA_WS (callbackObject)
//function FTA_WS ()
{
try {
	//this.callbackObject = null;
	this.callbackObject = callbackObject;
	this.ahc = new AsyncHttpConn (this.WEB_SERVICE_URL, this);

} catch (e) { alert ("FTA_WS (callbackObject) threw: " + e); }
}


FTA_WS.prototype.fetchLatestNews = function ()
{

	
	try {
		//use the google js api to load the spreadsheet data
		var feed = new google.feeds.Feed(this.GOOGLE_SPREADSHEET_URL);
		feed.setNumEntries(100);
		feed.load(this.callbackObject.showLatestNews);
	} catch (e) {
		alert ("fetchLatestNews() caught: " + e);
	}
}


FTA_WS.prototype.click = function (click_x, click_y, first_name, last_name, email, zip, country, keep_me_updated)
{

	var				request =	click_x + '\n' +
								click_y + "\n" +
								first_name + "\n" +
								last_name + "\n" +
								email + "\n" +
								zip + "\n" +
								country + "\n" +
								keep_me_updated + '\n';

try {
	this.ahc.send (request, this.INSERT_CLICK_URL, this.callbackObject.click);
} catch (e) { alert ("click() caught: " + e); }
}

FTA_WS.prototype.fccEmail = function (first_name, last_name, a1, a2, city, selectedState, zip, email, changed, submission)
{
	var				request =	first_name + "\n" + 
						last_name + "\n" + 
						a1 + "\n" + 
						a2 + "\n" + 
						city + "\n" + 
						selectedState + "\n" + 
						zip + "\n" +
						"US" + "\n" + 
						email + "\n" + 
						changed + "\n" + 
						submission + "\n";

	//alert (first_name + "\n" + last_name + "\n" + a1 + "\n" + a2 + "\n" + city + "\n" + selectedState + "\n" + zip + "\n" + email + "\n" + changed + "\n" + submission);

try {
	this.ahc.send (request, this.FCC_EMAIL_URL, this.callbackObject.fccEmail);
} catch (e) { alert ("fccEmail() caught: " + e); }
}

FTA_WS.prototype.last10 = function() {
	try {
		var date = new Date();
		var dateStamp = date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + "_" + (date.getHours()) + "-" + date.getMinutes() + "-" + date.getSeconds();
		
		var request = "";
		//this.ahc.sendGet (request, this.LAST_10_URL, this.callbackObject.last10);
		this.ahc.sendGet (request, (this.LAST_10_URL + "?" + dateStamp), this.callbackObject.last10);

	} 
	catch (e) { 
		alert ("last10() caught: " + e); 
	}
}

//
//	CALLBACK (PROTECTED)
//

//	Dispatches all responses from web service to the appropriate handler.

FTA_WS.prototype.ahcReply = function (message, callback)
{
	try {

		var		lines = message.split ('\n');
	
		//	Remove the last line (which is empty).
		lines.pop ();
	
		//	Parse response
		var		message = lines[0];
	
		try {
			//this.callbackObject.click (message);
			callback(message);
		
		} catch (e) {
			alert ("Callback for client-side handler threw an exception: " + e);
		}

	} catch (e) {
		//	Typically invoked if response format is corrupt.
		alert ("Caught exception in FTA_WS ahcReply (): " + e);
	}
}


//
//	PRIVATE
//


FTA_WS.prototype.WEB_SERVICE_URL = "/tile";
FTA_WS.prototype.FCC_EMAIL_URL = "/insertfcc";
FTA_WS.prototype.INSERT_CLICK_URL = "/insertclick";
FTA_WS.prototype.LAST_10_URL = "/last10";
FTA_WS.prototype.GOOGLE_SPREADSHEET_URL = "http://spreadsheets.google.com/feeds/list/pzfKM5cWQjYqxPgJWcACDBA/od6/public/values";

